﻿using System;


namespace RoughJs.Ast
{
    /// <summary>
    /// 2010-01-5 ----- 2010-01-29
    /// 陈曦 1.0
    /// Microsoft Public License (Ms-PL)
    ///This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
    ///1. Definitions
    ///The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
    ///A "contribution" is the original software, or any additions or changes to the software.
    ///A "contributor" is any person that distributes its contribution under this license.
    ///"Licensed patents" are a contributor's patent claims that read directly on its contribution.
    ///2. Grant of Rights
    ///(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
    ///(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
    ///3. Conditions and Limitations
    ///(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
    ///(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
    ///(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
    ///(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
    ///(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement. 
    /// 
    /// </summary>
    public class NativeScript:BaseFunction
    {
        private static  Object SCRIPT_TAG = "Script";
      public  static void init(Scriptable scope, bool sealed1)
    {
        NativeScript obj = new NativeScript(null);
        obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed1);
    }
      private NativeScript(Script script)
      {
          this.script = script;
      }
      public override string getClassName()
      {
          return "Script";
      }
      public override object call(Context cx, Scriptable scope, Scriptable thisObj, object[] args)
      {
          if (script != null)
          {
              return script.exec(cx, scope);
          }
          return Undefined.instance;
      }
      public override Scriptable construct(Context cx, Scriptable scope, Object[] args)
      {
          throw Context.reportRuntimeError0("msg.script.is.not.constructor");
      }
      public override int getLength()
      {
          return 0;
      }
      public override int getArity()
      {
          return 0;
      }
      public override string decompile(int indent, int flags)
      {
           if (script is NativeFunction) {
            return ((NativeFunction)script).decompile(indent, flags);
        }
        return base.decompile(indent, flags);
      }
      public override void initPrototypeId(int id)
      {
          String s;
          int arity;
          switch (id)
          {
              case Id_constructor: arity = 1; s = "constructor"; break;
              case Id_toString: arity = 0; s = "toString"; break;
              case Id_exec: arity = 0; s = "exec"; break;
              case Id_compile: arity = 1; s = "compile"; break;
              default: throw new ArgumentException(id.ToString());
          }
          initPrototypeMethod(SCRIPT_TAG, id, s, arity);
      }
      public override object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args)
      {
          if (!f.hasTag(SCRIPT_TAG))
          {
              return base.execIdCall(f, cx, scope, thisObj, args);
          }
          int id = f.methodId();
          switch (id)
          {
              case Id_constructor:
                  {
                      String source = (args.Length == 0)
                                      ? ""
                                      : ScriptRuntime.toString(args[0]);
                      Script script = compile(cx, source);
                      NativeScript nscript = new NativeScript(script);
                      ScriptRuntime.setObjectProtoAndParent(nscript, scope);
                      return nscript;
                  }

              case Id_toString:
                  {
                      NativeScript real = realThis(thisObj, f);
                      Script realScript = real.script;
                      if (realScript == null) { return ""; }
                      return cx.decompileScript(realScript, 0);
                  }

              case Id_exec:
                  {
                      throw Context.reportRuntimeError1(
                          "msg.cant.call.indirect", "exec");
                  }

              case Id_compile:
                  {
                      NativeScript real = realThis(thisObj, f);
                      String source = ScriptRuntime.toString(args, 0);
                      real.script = compile(cx, source);
                      return real;
                  }
          }
          throw new ArgumentException(id.ToString());
      }

      private static NativeScript realThis(Scriptable thisObj, IdFunctionObject f)
    {
        if (!(thisObj is NativeScript))
            throw incompatibleCallError(f);
        return (NativeScript)thisObj;
    }

      private static Script compile(Context cx, String source)
      {
          int[] linep = { 0 };
          String filename = Context.getSourcePositionFromStack(linep);
          if (filename == null)
          {
              filename = "<Script object>";
              linep[0] = 1;
          }
          ErrorReporter reporter;
          reporter = DefaultErrorReporter.forEval(cx.getErrorReporter());
          return cx.compileString(source, null, reporter, filename,
                                  linep[0], null);
      }
      public override int findPrototypeId(string s)
      {
          int id;

          id = 0; String X = null;
             switch (s.Length) {
            case 4: X="exec";id=Id_exec; goto L;
            case 7: X="compile";id=Id_compile; goto L;
            case 8: X="toString";id=Id_toString; goto L;
            case 11: X="constructor";id=Id_constructor; goto L;
            }
         L: ;
            if (X!=null && X!=s && !(X==s)) id = 0;
            goto L0;
        
          L0:;

        return id;
      }

         private const  int
        Id_constructor    = 1,
        Id_toString       = 2,
        Id_compile        = 3,
        Id_exec           = 4,
        MAX_PROTOTYPE_ID  = 4;



    private Script script;
    }// end for class
}
